Component({
  behaviors: ['wx://form-field'],

  externalClasses: ['i-class'],

  properties: {
    title: {
      type: String
    },
    // text || textarea || password || number
    type: {
      type: String,
      value: 'text'
    },
    disabled: {
      type: Boolean,
      value: false
    },
    placeholder: {
      type: String,
      value: ''
    },
    autofocus: {
      type: Boolean,
      value: false
    },
    mode: {
      type: String,
      value: 'normal'
    },
    right: {
      type: Boolean,
      value: false
    },
    error: {
      type: Boolean,
      value: false
    },
    maxlength: {
      type: Number,
      value:-1
    },
    key: {
      type: String
    },
    isMust: {
      type: Boolean,
      value: false
    },
    isReplaceEmoji:{
      type:Boolean,
      value:false
    },
    value: {
      type: String
    }
  },

  methods: {
    handleInputChange(event) {
      const {
        detail = {}
      } = event;
      const {
        value = ''
      } = detail;
      this.setData({
        value
      });

      let param = {
        key: this.data.key,
        value: value
      }
      this.checkIsMustLimit()
      this.checkIsReplaceEmoji()
      this.triggerEvent('change', param);
      // this.triggerEvent('change', event);
    },

    handleInputFocus(event) {
      this.triggerEvent('focus', event);
    },

    handleInputBlur(event) {
      this.checkIsMustLimit()
      this.triggerEvent('blur', event);
    },
    checkIsReplaceEmoji(){
      if (this.data.isReplaceEmoji){
        /**检查value是否为emoji */
        let v = this.data.value
        if (v || v != '') {
          let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])/g;
          if (reg.test(v)){
              v =  v.replace(reg, '');
              this.setData({
                value: v
              });
          }
        }
      }
    },
    /**必填检验 */
    checkIsMustLimit() {
      /**必填字段 */
      if (this.data.isMust) {
        /**检查value是否为空 */
        let v = this.data.value
        if (!v || v == '') {
          if (!this.data.error) {
            this.setData({
              error: true
            })
          }
        } else {
          if (this.data.error) {
            this.setData({
              error: false
            })
          }
        }
      }
    }
  }
});